//package com.zmn.plat.admin.advice;
//
//import com.alibaba.fastjson.JSON;
//import com.zmn.common.dictionary.GlobalDict;
//import com.zmn.common.utils.date.DateUtil;
//import com.zmn.common.utils.request.RequestUtil;
//import com.zmn.common.utils.string.StringUtil;
//import com.zmn.manager.shiro.dto.UserTokenDO;
//import com.zmn.manager.shiro.interfaces.ShiroTokenManager;
//import com.zmn.plat.manager.interfaces.mq.SysLogMqManager;
//import com.zmn.plat.manager.interfaces.shiro.ShiroManager;
//import com.zmn.plat.model.entity.log.SysLog;
//import com.zmn.plat.model.entity.mc.staff.StaffToken;
//import org.aspectj.lang.annotation.*;
//import org.slf4j.Logger;
//import org.aspectj.lang.JoinPoint;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//
///**
// * 记录系统日志
// * 发送到消息队列
// * @author nowind
// * @since 2017/9/20 11:44
// */
////@Component
////@Aspect
//public class SysLogAdvice {
//    private Logger logger = LoggerFactory.getLogger(SysLogAdvice.class);
//
//    @Resource
//    private ShiroManager shiroManager;
//    @Resource
//    private ShiroTokenManager shiroTokenManager;
//    @Autowired
//    private SysLogMqManager sysLogMqSendManager;
//
//    /**
//     * 定义切点
//     */
//    @Pointcut("execution(* com.zmn.plat.services.interfaces.*..*Service.insert*(..))")
//    public void insert() { }
//    @Pointcut("execution(* com.zmn.plat.services.interfaces.*..*Service.update*(..))")
//    public void update() { }
//    @Pointcut("execution(* com.zmn.plat.services.interfaces.*..*Service.delete*(..))")
//    public void delete() { }
//
//    //region 数据更新/新增日志记录（公用）
//
//    @After("insert() || update() || delete()")
//    public void doAfter(JoinPoint jp) {
//
//        HttpServletRequest request = getRequest(jp);
//        if(request == null) {
//            return;
//        }
//
//        //String methodName = jp.getTarget().getClass().getName() + "." + jp.getSignature().getName();
//        String methodName = jp.getSignature().toString();
//        String methodParams = "";
//        Object[] args = jp.getArgs();
//        if (args != null) {
//            methodParams = JSON.toJSONString(args);
//        }
//
//        String content = String.format("data=%s, url=%s", methodParams, getRequestUrl(request));
//
//        SysLog log = buildLog(GlobalDict.LOG_TYPE_SERVICE, methodName, content);
//        bindRequestAttributes(request, log);
//
//        sysLogMqSendManager.sendQueue(log);
//    }
//    //endregion
//
//    //region 异常日志记录（公用）
//
//    @AfterThrowing(pointcut = "insert() || update() || delete()", throwing ="ex")
//    public void doThrowing(JoinPoint jp, Exception ex) {
//
//        HttpServletRequest request = getRequest(jp);
//        if(request == null) {
//            return;
//        }
//
//        String methodName = jp.getSignature().toString();
//        String methodParams = "";
//        Object[] args = jp.getArgs();
//        if (args != null) {
//            methodParams = JSON.toJSONString(args);
//        }
//
//        String content = String.format("params=%s, url=%s, ex=%s", methodParams, getRequestUrl(request), ex.getMessage());
//
//        SysLog log = buildLog(GlobalDict.LOG_TYPE_EXCEPTION, methodName, content);
//        bindRequestAttributes(request, log);
//
//        sysLogMqSendManager.sendQueue(log);
//
//    }
//    //endregion
//
//
//    //region buildLog
//    private SysLog buildLog(int type, String methodName, String content) {
//
//        SysLog log = new SysLog();
//        log.setType(type);
//        log.setMethodName(methodName);
//        log.setContent(content);
//        //log.setAboutid(aboutId);
//        //log.setAboutstatus(aboutStatus);
//        //log.setOperatortype(operatorType);
//        //log.setOperatorip(operatorIp);
//        //log.setOperatorid(operatorId);
//        //log.setOperator(operator);
//        log.setCreateTime(DateUtil.getNow());
//
//        return log;
//    }
//    //endregion
//
//    //region 获取request中设置的信息
//    private void bindRequestAttributes(HttpServletRequest request, SysLog log) {
//
//        UserTokenDO token = shiroTokenManager.getUserToken();
//
//        if (token != null) {
//            StaffToken staff = shiroManager.parseObject(token);
//            log.setOperatorId(staff.getStaffId());
//            log.setOperator(staff.getStaffName());
//        }
//
//        Object moduleId = request.getAttribute("moduleId");
//        Object aboutId = request.getAttribute("aboutId");
//        Object aboutSatus = request.getAttribute("aboutSatus");
//        Object operatorType = request.getAttribute("operatorType");
//
//        if(moduleId != null && log.getModuleId() == 0) {
//            log.setModuleId(Integer.parseInt(moduleId.toString()));
//        }
//        if(aboutId != null && StringUtil.isBlank(log.getAboutId())) {
//            log.setAboutId(aboutId.toString());
//        }
//        if(aboutSatus != null && log.getAboutStatus() == 0) {
//            log.setAboutStatus(Integer.parseInt(aboutSatus.toString()));
//        }
//        if(operatorType != null && log.getOperatorType() == 0) {
//            log.setOperatorType(Integer.parseInt(operatorType.toString()));
//        }
//
//        log.setOperatorIP(RequestUtil.getRequestIp(request));
//    }
//    //endregion
//
//    //region 获得Request中的Url和Query信息
//    private String getRequestUrl(HttpServletRequest request) {
//
//        String url = request.getRequestURL().toString();
//        String params = request.getQueryString();
//        params = !StringUtil.isBlank(params) ? ("?" + params) : "";
//        url += params;
//
//        return url;
//    }
//    //endregion
//
//    //region 获取request
//    private HttpServletRequest getRequest(JoinPoint jp) {
//
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        if(requestAttributes == null) {
//            return null;
//        }
//
//        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
//        if(request == null) {
//            return null;
//        }
//
//        return request;
//    }
//    //endregion
//
//}
